package com.process.controller;

import com.auth.common.result.Result;
import com.auth.model.process.Process;
import com.auth.model.process.ProcessType;
import com.auth.service.SysUserService;
import com.auth.vo.process.ApprovalVo;
import com.auth.vo.process.ProcessFormVo;
import com.auth.vo.process.ProcessVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.process.service.OaProcessService;
import com.process.service.OaProcessTemplateService;
import com.process.service.OaProcessTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:Tangjiachang
 * @date 2023/7/23
 * @description: 用户端，审批流程管理入口
 * @CrossOrigin 注解解决跨域问题
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping(value="/admin/process")
@CrossOrigin
public class ProcessController {
    private final OaProcessTypeService processTypeService;
    private final OaProcessTemplateService processTemplateService;
    private final OaProcessService processService;
    private final SysUserService sysUserService;
    public ProcessController(OaProcessTypeService processTypeService,
                             OaProcessTemplateService processTemplateService,
                             OaProcessService processService, SysUserService sysUserService) {
        this.processTypeService = processTypeService;
        this.processTemplateService = processTemplateService;
        this.processService = processService;
        this.sysUserService = sysUserService;
    }

    @ApiOperation("查询所有审批分类和每个分类所有审批模版")
    @GetMapping("/findProcessType")
    public Result findProcessType() {
        List<ProcessType> list = processTypeService.findProcessType();
        return Result.ok(list);
    }

    @ApiOperation("获取审批模版数据")
    @GetMapping("/getProcessTemplate/{processTemplateId}")
    public Result getProcessTemplate(@PathVariable Long processTemplateId) {
        return Result.ok(processTemplateService.getById(processTemplateId));
    }

    @ApiOperation("启动流程")
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo) {
        processService.startUp(processFormVo);
        return Result.ok();
    }

    @ApiOperation("获取待处理列表")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(@PathVariable Long page,
                              @PathVariable Long limit) {
        Page<ProcessVo> pageModel = processService.findPending(new Page<Process>(page, limit));
        return Result.ok(pageModel);
    }

    @ApiOperation("查询审批信息")
    @GetMapping("/show/{id}")
    public Result show(@PathVariable Long id) {
        return Result.ok(processService.show(id));
    }

    @ApiOperation("审批")
    @PostMapping("/approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        processService.approve(approvalVo);
        return Result.ok();
    }

    @ApiOperation("流程已处理接口")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(@PathVariable Long page,
                                @PathVariable Long limit) {
        return Result.ok(processService.findProcessed(new Page<Process>(page, limit)));
    }

    @ApiOperation("流程已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(@PathVariable Long page,
                              @PathVariable Long limit) {
        return Result.ok(processService.findStarted(new Page<ProcessVo>(page, limit)));
    }

    @ApiOperation("获取当前用户基本信息")
    @GetMapping("/getCurrentUser")
    public Result getCurrentUser() {
        return Result.ok(sysUserService.getCurrentUser());
    }
}
